package com.bc.commonentity.log;


import com.bc.commonentity.common.AppJsonResult;
import com.bc.commonentity.common.AppJsonResultData;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxrs.impl.ResponseImpl;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageContentsList;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import javax.ws.rs.core.Response;
import java.util.List;

public class AppRequestInterceptor extends AbstractPhaseInterceptor<Message> {
    public AppRequestInterceptor(String phase) {
        super(phase);
    }

    public AppRequestInterceptor() {
        super(Phase.PRE_STREAM);
    }

    /**
     * <请求拦截器> 创 建 人: zhaohailong 创建时间: 20151020
     *
     * @param message
     * @throws Fault
     */
    public void handleMessage(Message message) throws Fault {
        AspectLogHelper.cxfAfterCompletion(message);
        //将JsonResult返回值转换为AppJsonResult
        Exchange exchange = message.getExchange();
        Message outMessage = exchange.getOutMessage();
        if(outMessage==null){
            return;
        }
        MessageContentsList messageContentsList = (MessageContentsList) outMessage.getContent(List.class);
        JsonResult js= getJsonResult(messageContentsList);
        if(js!=null){
            AppJsonResult appJsonResult=getAppJsonResult(js);
            messageContentsList.remove(0);
            messageContentsList.add(0, appJsonResult);
        }
    }

    /**
     * 从cxf的message中获取业务返回的JsonResult
     * @param messageContentsList
     * @return
     */
    private static JsonResult getJsonResult(MessageContentsList messageContentsList){
        if(messageContentsList==null||messageContentsList.size()==0){
            return null;
        }
        //系统异常时，返回值的类型为ResponseImpl
        if (messageContentsList.get(0) instanceof ResponseImpl) {
            Object responseEntity=((ResponseImpl)messageContentsList.get(0)).getEntity();
            if (responseEntity instanceof JsonResult) {
                return (JsonResult)responseEntity;
            }
        }else if (messageContentsList.get(0) instanceof JsonResult) {
            return (JsonResult)messageContentsList.get(0);
        }
        return null;
    }

    /**
     * 将JsonResult返回值转换为AppJsonResult
     * 业务码或msg是系统异常时，响应码改为500,
     * 业务码是未登录时，响应码改为400
     * 业务码是其他值时，响应码改为200
     * @param originalResult
     * @return
     */
    private static AppJsonResult getAppJsonResult(JsonResult originalResult){
        if(originalResult==null){
            return null;
        }
        int totalCode=Response.Status.OK.getStatusCode();
        if(originalResult.getCode()== Response.Status.BAD_REQUEST.getStatusCode()){
            totalCode=Response.Status.BAD_REQUEST.getStatusCode();
        }else if(originalResult.getCode()== HttpReturnRnums.SystemError.value()){
            totalCode=Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
        }else if(originalResult.getMsg()!=null&&
                originalResult.getMsg().equals(HttpReturnRnums.SystemError.desc())){
            totalCode=Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
        }

        AppJsonResult result=new AppJsonResult();
        result.setCode(totalCode);
        if (totalCode==Response.Status.OK.getStatusCode()) {
            result.setMsg(HttpReturnRnums.Success.desc());
            AppJsonResultData content=new AppJsonResultData();
            content.setBusinessCode(originalResult.getCode());
            content.setMessage(originalResult.getMsg());
            if (originalResult.getData()!=null) {
                content.setData(originalResult.getData());
            }
            result.setContent(content);
        }else{
            result.setMsg(HttpReturnRnums.SystemError.desc());
        }
        return result;
    }
}

